import { createRouter, createWebHistory } from 'vue-router'
import layout from '@/layout/Layout.vue'
import { ElMessage, ElNotification } from 'element-plus'
import { useStaffStore } from "@/stores/staff.js"
import { useNavigationStore } from '@/stores/navigation'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: layout,
      redirect: "/index",
      children: [
        {
          path: '/index',
          name: "首页",
          component: () => import('../views/Index.vue'),
        },
        {
          path: '/staff',
          name: "员工管理",
          component: () => import('../views/system/staff/Staff.vue')
        },
        {
          path: '/role',
          name: "角色管理",
          component: () => import('../views/system/role/Role.vue')
        },
        {
          path: '/menu',
          name: "菜单管理",
          component: () => import('../views/system/menu/Menu.vue')
        },
        {
          path: '/organization',
          name: "组织管理",
          component: () => import('../views/system/organization/Organization.vue')
        },
        {
          path: '/announcement',
          name: "公告管理",
          component: () => import('../views/system/announcement/Announcement.vue')
        },
        {
          path: '/staffInfo',
          name: "个人信息",
          component: () => import('../views/system/staff/StaffInfo.vue')
        },
        {
          path: '/classManagement/attendance',
          name: "考勤管理",
          component: () => import('../views/classes/attendance/Attendance.vue')
        },
        {
          path: '/classManagement/exam',
          name: "考试管理",
          component: () => import('../views/classes/exam/ExamView.vue')
        },
        {
          path: '/course',
          name: '课程维护',
          component: () => import("@/views/subject/course/courseList.vue")
        },
        {
          path: '/classManagement/student',
          name: "学生管理",
          component: () => import('../views/classes/student/StudentView.vue')
        },
        {
          path: '/Register',
          name: "扫码注册",
          component: () => import('../views/classes/student/RegisterView.vue')
        },
        {
          path: '/questionBank',
          name: "题库管理",
          component: () => import('../views/question/questionBank/QuestionBankView.vue')
        },
        {
          path: '/questionBank/questions/:id',
          name: "题目管理",
          component: () => import('../views/question/questions/QuestionView.vue')
        },
        {
          path: '/questionBank/questions/:questionBankId/saveOrUpdateQuestion/:id',
          name: '添加/修改题目',
          component: () => import("../views/question/questions/QuestionSaveOrUpdate.vue")
        },
        {
          path: '/classManagement/exam/createExam',
          name: '创建考试',
          component: () => import("../views/classes/exam/CreateExamView.vue")
        },
        {
          path: '/classManagement',
          name: '班级管理',
          component: () => import("@/views/classes/classcourse/ClassCourseProgress.vue"),
        },
        {
          path: '/classManagement/classManagementProgress',
          name: '课程信息',
          component: () => import("@/views/classes/classcourse/ClassManagementProgress.vue")
        },
        {
          path: '/subject',
          name: '学科维护',
          component: () => import("../views/subject/subject/SubjectList.vue")
        },
        {
          path: '/homework',
          name: '作业维护',
          component: () => import("@/views/subject/course/homeworkList.vue")
        },
        {
          path: '/class',
          name: "班级维护",
          component: () => import('../views/subject/classes/Class.vue')
        }, {
          path: '/classManagement/employment',
          name: "就业管理",
          component: () => import('../views/classes/employment/EmploymentView.vue')
        },
        {
          path: '/classManagement/workandcorrect',
          name: "作业管理",
          component: () => import('../views/classes/workandcorrect/Work.vue')
        },
        {
          path: '/VerifyQuestion',
          name: "题目审批",
          component: () => import('../views/verify/VerifyQuestion.vue')
        },
        {
          path: '/VerifyQuestionBank',
          name: "题库审批",
          component: () => import('../views/verify/VerifyQuestionBank.vue')
        },
        {
          path: '/VerifyLeaveApplication',
          name: "考勤审批",
          component: () => import('../views/verify/VerifyLeaveApplication.vue')
        },
        {
          path: '/classManagement/workandcorrect/StudentWork',
          name: '学生作业',
          component: () => import('../views/classes/workandcorrect/StudentWork.vue')
        },{
          path: '/classManagement/exam/correctExamView',
          name: "试卷批改",
          component: () => import('../views/classes/exam/CorrectExamView.vue')
        },{
          path: '/ClassWork',
          name: "课程作业",
          component: () => import('../views/classes/classcourse/ClassWork.vue')
        }
      ]
    },
    {
      path: '/login', name: "login", component: () => import("@/views/LoginView.vue")
    },
    {
      path:'/messageOfRegister',
      name:'注册信息填写',
      component: () => import('../views/classes/student/messageOfRegister.vue')
    },{
      path:'/RegisterScussesView',
      name:'注册成功',
      component: () => import('../views/classes/student/RegisterScussesView.vue')
    },
    // 404
    {
      path: "/found404",
      name: "found404",
      component: () => import("@/views/NotFound.vue"),
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: "/found404",
      hidden: true,
    },
  ]
})
router.beforeEach((to, from, next) => {
  let navigation = useNavigationStore()

  if(to.path == '/index' || to.path == '/'){
    navigation.clearPath()
  }

  if(to.path == '/staffInfo'){
    navigation.clearPath()
    navigation.savePath(to.path,to.name,to.query)
  }

  if (to.path != '/index' && to.path != '/') {
    navigation.savePath(to.path, to.name,to.query)
  }

  if (localStorage.getItem("token") == null && to.path !== '/login' && to.path !== '/messageOfRegister' && to.path !== '/RegisterScussesView') {
    ElMessage.error('请先登录')
    router.push("/login")
  } else {
    if (to.path !== '/staffInfo' && to.path !== '/login' && to.path !== '/messageOfRegister' && to.path !== '/RegisterScussesView' && useStaffStore().staffInfo.staff.isFirstLogin === 1) {
      ElNotification({
        title: '提示',
        message: '首次登陆，请修改密码',
        type: 'warning',
      })
      next('/staffInfo') // 重定向到 /staffInfo
    } else {
      next()
    }
  }

})
export default router
